package com.topway.dsmflex.ui.composite.servers;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.topway.dsmflex.Alert;
import com.topway.dsmflex.model.servers.DataServerBean;

public class AddServersMonitor extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text txtName;
	private DataServerBean dataServerBean;
	   
	   private Text txtNterval;
	   private Button checkEnabled;
	   
	   private String btnType = "";
	/**
	 * Create the dialog.
	 * @param parentble
	 * @param style
	 */
	public AddServersMonitor(Shell parent, int style,DataServerBean dataServerBean) {
		super(parent, style);
		
		this.dataServerBean = dataServerBean;
	}

	   public int initForm() {
				if(null==dataServerBean)
				{
					shell.setText("添加服务器监控配置");
					this.dataServerBean = new DataServerBean();
				}
				else
				{
					InitDataTableBean();
					shell.setText("修改服务器监控配置["+dataServerBean.getName()+"]");
				}
				 return 1;
	   }
	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();

		initForm();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return dataServerBean;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
	    shell = new Shell(getParent(),SWT.DIALOG_TRIM  );
		shell.setSize(539, 269);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
	    shell.setLocation(getParentLocation());
	    
		SashForm sashForm = new SashForm(shell, SWT.VERTICAL);
		
		Group grpSql = new Group(sashForm, SWT.NONE);
		grpSql.setLayout(null);
		
		Label lblName = new Label(grpSql, SWT.NONE);
		lblName.setBounds(92, 23, 36, 12);
		lblName.setText("名称：");
		
		txtName = new Text(grpSql, SWT.BORDER);
		txtName.setBounds(134, 20, 184, 18);
		
		Label label = new Label(grpSql, SWT.NONE);
		label.setBounds(80, 54, 48, 12);
		label.setText("服务器：");
		
		Label label_2 = new Label(grpSql, SWT.NONE);
		label_2.setBounds(360, 200, 30, 12);
		label_2.setText("端口：");
		label_2.setVisible(false);
		
		Label label_8 = new Label(grpSql, SWT.NONE);
		label_8.setBounds(336, 24, 36, 12);
		label_8.setText("频率：");
	
		Button btnNewButton = new Button(grpSql, SWT.NONE);
		btnNewButton.setBounds(166, 171, 71, 22);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnType="SUBMIT";
				int reuslt = SaveDataTableBean();
				if(reuslt>0)
				shell.close();
			}

		});
		btnNewButton.setText("确定");
		
		txtNterval = new Text(grpSql, SWT.BORDER);
		txtNterval.setBounds(378, 21, 60, 18);
		txtNterval.setText("10.0");
		
		Label label_9 = new Label(grpSql, SWT.NONE);
		label_9.setText("（分）");
		label_9.setBounds(445, 24, 36, 12);
		
		Button btnNewButton_1 = new Button(grpSql, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
					dataServerBean=null;
					shell.close();
			}
		});
		btnNewButton_1.setBounds(293, 171, 72, 22);
		btnNewButton_1.setText("取消");
		
		checkEnabled = new Button(grpSql, SWT.CHECK);
		checkEnabled.setSelection(true);
		checkEnabled.setBounds(80, 149, 93, 16);
		checkEnabled.setText("激活");
		
		txtIp = new Text(grpSql, SWT.BORDER);
		txtIp.setBounds(134, 51, 304, 18);
		
		txtPort = new Text(grpSql, SWT.BORDER);
		txtPort.setBounds(403, 197, 60, 18);
		txtPort.setVisible(false);
		
		txtRemark = new Text(grpSql, SWT.BORDER);
		txtRemark.setBounds(134, 85, 304, 50);
		
		Label label_1 = new Label(grpSql, SWT.NONE);
		label_1.setText("描述：");
		label_1.setBounds(92, 98, 36, 12);


		sashForm.setWeights(new int[] { 440 });
		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent e) {
				if (btnType.equals(""))
					dataServerBean = null;
				e.doit = true;
			}
		});
		    

	}
	
	private void InitDataTableBean()
	{

		this.txtName.setText(null==this.dataServerBean.getName()?"":this.dataServerBean.getName());
		txtIp.setText(dataServerBean.getIp());
		txtPort.setText(dataServerBean.getPort());
		txtRemark.setText(dataServerBean.getRemark());
		txtNterval.setText(""+dataServerBean.getSynchroNterval());
		
	}
	   public Point getParentLocation() {

		      Shell shell = getParent();
		      int x = shell.getLocation().x;
		      int y = shell.getLocation().y;
		      Rectangle rectParent = shell.getBounds();
		      Point size = this.shell.getSize();
		      int xx = x + rectParent.width / 2 - size.x / 2;
		      int yy = y + rectParent.height / 2 - size.y / 2;
		      return new Point(xx, yy);
		   }
	   
	   
	public int SaveDataTableBean()
	{
		this.dataServerBean.setName(this.txtName.getText());
		this.dataServerBean.setEnabled(checkEnabled.getSelection()?1:0);
		this.dataServerBean.setIp(this.txtIp.getText());
		this.dataServerBean.setPort(this.txtPort.getText());
		this.dataServerBean.setRemark(this.txtRemark.getText());
		try {
			this.dataServerBean.setSynchroNterval(Float.valueOf(txtNterval.getText()));
		} catch (Exception e) {
			Alert.MsgBox("提示", "频率有误，请检查！");
			txtNterval.forceFocus();
			return 0;
		}
		if(null==dataServerBean.getName()||dataServerBean.getName().equals(""))
		{
			Alert.MsgBox("提示", "任务名必填，请检查！");
			txtName.forceFocus();
			return 0;
		}
		return 1;
		
	}
	
	
	   private Text txtIp;
	   private Text txtPort;
	   private Text txtRemark;
	  
}
